#!/bin/bash
#Barry Kauler, Oct 2010
#3builddistro to call this.
#there is a problem with packages, say abiword, abiword_DEV, abiword_DOC, abiword_NLS
#maybe each being in a different category. see info:
#puppy package database format:
#pkgname|nameonly|version|pkgrelease|category|size|path|fullfilename|dependencies|description|compileddistro|compiledrelease|repo|
#...'compileddistro|compiledrelease' (fields 11,12) identify where the package was compiled.
#in the PPM it is better if they are grouped in same category.
#120812 db category now allows optional subcategory (for which an icons exists in /usr/local/lib/X11/mini-icons).
#ie: pkgname|nameonly|version|pkgrelease|category[;category]|size|path|fullfilename|dependencies|description|compileddistro|compiledrelease|repo|

#ex: before:
#homebank-4.3-w5|homebank        |4.3-w5||Business|604K||homebank-4.3-w5.pet|+gtk+|HomeBank finance management|puppy|wary5||
#homebank_DOC-4.3-w5|homebank_DOC|4.3-w5||BuildingBlock|1892K||homebank_DOC-4.3-w5.pet||personal finance||||
#homebank_NLS-4.3-w5|homebank_NLS|4.3-w5||BuildingBlock|1972K||homebank_NLS-4.3-w5.pet|+homebank|personal finance||||
#after:
#homebank-4.3-w5|homebank        |4.3-w5||Business|604K||homebank-4.3-w5.pet|+gtk+|HomeBank finance management|puppy|wary5||
#homebank_DOC-4.3-w5|homebank_DOC|4.3-w5||Business|1892K||homebank_DOC-4.3-w5.pet||personal finance||||
#homebank_NLS-4.3-w5|homebank_NLS|4.3-w5||Business|1972K||homebank_NLS-4.3-w5.pet|+homebank|personal finance||||

[ ! -f $1 ] && exit 1
DBFILE="$1" #ex: Packages-puppy-wary5-official

echo "Rationalizing categories in ${DBFILE}, please wait..."

(
while read ONEDBENTRY
do
	ENTRYfixed="$ONEDBENTRY"
	IFS="|" read -r F1 F2 F3 F4 F5 ETC <<< "$ONEDBENTRY"
	DB_nameonly="$F2"
	case "$DB_nameonly" in *"_DEV"*|*"_DOC"*|*"_NLS"*)
		#big assumption that these are listed in db afterward...
		nameonlybase="${DB_nameonly%_*}" #remove the '_DEV', '_DOC', '_NLS' part...
		#echo "nameonlyref=$nameonlyref nameonlybase=$nameonlybase" #TEST
		if [ "$nameonlybase" = "$nameonlyref" ];then
													#F5
			ENTRYfixed="${F1}|${F2}|${F3}|${F4}|${DB_category}|${ETC}"
		fi
		;;
	*)
		DB_category=${F5%%;*} #120812
		nameonlyref="$DB_nameonly"
		;;
	esac
	echo "$ENTRYfixed"
done < $DBFILE
) > /tmp/rationalized_categories
exit 0

###END###
